jq$ = jQuery.noConflict();
var searchTarget = 1;

function dispSettings()
{
  loadSettings();
  
  var elm = document.getElementById("searchTarget");
  for (var i = 0; i < elm.options.length; i++) { 
    if (elm.options[i].value == searchTarget) {
      elm.options[i].selected = true;
    }
  
  }
}

function dispTrendWords()
{
  loadSettings();
  
  // 急上昇ワード取得URL
  var url = 'http://www.google.jp/m/services/trends/get';

  var params = {};

  params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.GET;
  params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.DOM;
  params[gadgets.io.RequestParameters.AUTHORIZATION] = gadgets.io.AuthorizationType.NONE;

  gadgets.io.makeRequest(
    url,
    function (response)
    {
      var searchBaseUrl = 'http://mixi.jp/search_community.pl?type=com&submit=main&category_id=0&sort=member&per_page=10&keyword=';
      var xml = response.data;
      var queryList = new Array();
      var i = 0;
      
      jq$(xml).find("query").each(
        function ()
        {
          queryList[i] = jq$(this).text();
          i++;
        }
      );
      
      var list = '<table border="0" cellspacing="1" cellpadding="3" bgcolor="#FEE7C7" style="font-size: 9pt;">'
               + '<tr color="#333333">'
               + '  <td>順位</td>'
               + '  <td>キーワード</td>'
               + '</tr>';

      for (i = 0; i < queryList.length; i++) {
        list += '<tr color="#333333" bgcolor="#FFFFFF">'
              + '<td>' + (i + 1) + '</td>'
              + '<td><a href="#" onclick="doSearch(\'' + queryList[i] + '\');">' + queryList[i] + '</a></td>'
              + '</tr>';
      }

      list += '</table>';
      document.getElementById("trend").innerHTML = list;
    },
    params
  );

}

function doSearch(keyword)
{
  document.charset='Shift_JIS';
  
  if (searchTarget == 1) {
    var searchForm = document.getElementById("comSearchForm");
    searchForm.comKeyword.value = keyword;
  } else if (searchTarget == 2) {
    var searchForm = document.getElementById("diaSearchForm");
    searchForm.diaKeyword.value = keyword;
  }

  searchForm.submit();

  document.charset='UTF-8';
}

function loadSettings()
{
  var req = opensocial.newDataRequest();
  req.add(req.newFetchPersonRequest("VIEWER"), "viewer");
  req.add(req.newFetchPersonAppDataRequest("VIEWER", ["searchTarget"]), "viewer_data");
  req.send(loadSettingsResponse);
}

function loadSettingsResponse(data)
{
  var viewer = data.get("viewer").getData();
  var viewer_data = data.get("viewer_data");
  var dataAry = viewer_data.getData()[viewer.getId()];
  
  searchTarget = (dataAry == null) ? "1" : dataAry["searchTarget"];
}

function saveSettings()
{
  var elm = document.getElementById("searchTarget");
  var val = elm.options[elm.selectedIndex].value;
    
  var req = opensocial.newDataRequest();
  req.add(req.newUpdatePersonAppDataRequest("VIEWER", "searchTarget", val));
  req.send(saveSettingsResponse);
}

function saveSettingsResponse()
{
  alert("保存しました。");
}